/**
 * Date: 2023-04-23
 * Time: 16:10
 */




// 定义年龄非法异常类
class AgeException extends RuntimeException {
    public AgeException(String message) {
        super(message);
    }
}

// 定义车辆类
class Vehicle {
    private String type; // 车型

    public Vehicle(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }
}

// 定义驾驶员类
class Driver {
    private int age; // 年龄
    private String licenseType; // 驾照类型

    public Driver(int age, String licenseType) {
        this.age = age;
        this.licenseType = licenseType;
    }

    public int getAge() {
        return age;
    }

    public String getLicenseType() {
        return licenseType;
    }

    public void drive(Vehicle vehicle) throws AgeException {
        // 判断驾照类型和年龄是否符合要求
        if (licenseType.equals("A1") && (age < 26 || age > 50)) {
            throw new AgeException("A1驾照年龄要求：26岁至50岁之间");
        } else if (licenseType.equals("A2") && age < 24) {
            throw new AgeException("A2驾照年龄要求：24岁及以上");
        } else if (licenseType.equals("B1") && age < 18) {
            throw new AgeException("B1驾照年龄要求：18岁及以上");
        } else {
            System.out.println("您已使用" + licenseType + "驾照驾驶" + vehicle.getType());
        }
    }
}

// 测试类
public class Test3 {
    public static void main(String[] args) {
        Driver driver1 = new Driver(25, "A1"); // A1驾照，年龄不合法
        Driver driver2 = new Driver(30, "A1"); // A1驾照，年龄合法
        Driver driver3 = new Driver(20, "A2"); // A2驾照，年龄合法
        Driver driver4 = new Driver(16, "B1"); // B1驾照，年龄不合法

        Vehicle vehicle1 = new Vehicle("轿车");
        Vehicle vehicle2 = new Vehicle("摩托车");

        try {
            driver1.drive(vehicle1);
        } catch (AgeException e) {
            System.out.println("驾驶员年龄非法：" + e.getMessage());
        }
        try {
            driver2.drive(vehicle1);
        } catch (AgeException e) {
            System.out.println("驾驶员年龄非法：" + e.getMessage());
        }
        try {
            driver3.drive(vehicle2);
        } catch (AgeException e) {
            System.out.println("驾驶员年龄非法：" + e.getMessage());
        }
        try {
            driver4.drive(vehicle2);
        } catch (AgeException e) {
            System.out.println("驾驶员年龄非法：" + e.getMessage());
        }
    }
}
